#include <iostream>
#include <unistd.h>
#include "Thread.hpp"
using namespace mythread;


// about mutex
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int tickets = 10000;
void Rounte(const std::string& name)
{
    while(true)
    {
        pthread_mutex_lock(&mutex);
        if(tickets > 0)
        {
            usleep(1000);
            printf("who: %s, get a ticket: %d\n", name.c_str(), tickets);
            tickets--;
            pthread_mutex_unlock(&mutex);
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            break;
        }
    }
}
int main()
{
    Thread t1("thread -1", Rounte);
    Thread t2("thread -2", Rounte);
    Thread t3("thread -3", Rounte);
    Thread t4("thread -4", Rounte);

    t1.Start();
    t2.Start();
    t3.Start();
    t4.Start();

    t1.Join();
    t2.Join();
    t3.Join();
    t4.Join();
    return 0;
}